/**
 * 
 */
package org.codesketch.collector.common;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.codesketch.collector.domain.Record;
import org.springframework.stereotype.Component;

/**
 * @author Quirno Brizi
 *
 */
@Component
public class Broker {
	/*
	 * Use LinkedBlockingQueue instead of SynchronousQueue because the put method on
	 * SynchronousQueue will not return until there is an invocation for the take 
	 * method.
	 */
	private BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
	
	/**
	 * Retrieves and removes the head of this queue, waiting 
	 * if necessary until an element becomes available. 
	 * @return a {@link Record} as XML string
	 * @throws InterruptedException
	 */
	public String take() throws InterruptedException {
		return queue.take();
	}
	/**
	 * Inserts the specified element into this queue, waiting 
	 * if necessary for space to become available.
	 * @param record the {@link Record} to have to insert as XML string
	 * @throws InterruptedException
	 */
	public void put(final String record) throws InterruptedException {
		if(null!=record) {
			queue.put(record);
		}
	}
}
